<?php

/**
 * Specify group object. Group table.
 */
class group
{
    /**
     * int group id
     */
    var $id;

    /**
     * string group name
     */
    var $name;

    /**
     * string group description
     */
    var $description;

    /**
     * int group is hidden
     */
    var $hidden;
}

/**
 * Get groups.
 *
 * @param $p_sort_field string sort field
 * @param $p_sort_order string sort order: ASC or DESC
 * @param $p_page int page number
 * @param $p_count int items count on the page
 * @param $p_filter class book_filter
 *
 * @return array groups array
 */
function get_groups($p_sort_field = 'id',
            $p_sort_order = 'ASC',
            $p_page = 1,
            $p_count = 0,
            $p_filter = NULL
            )
{
    $t_sort_field = db_escape_string($p_sort_field);
    $t_sort_order = db_prepare_sort_order($p_sort_order);
    $t_page = db_prepare_int($p_page);
    $t_count = db_prepare_int($p_count);

    if ($t_sort_field == '')
        $t_sort_field = 'id';

    if (!in_array($t_sort_field, array('id', 'group_name',
            'group_description', 'group_hidden')))
    {
        $t_sort_field = 'id';
    }

    $order_str = "`$t_sort_field` $t_sort_order";

    if ($p_count != 0)
    {
        $limit = ($t_page-1)*$t_count;
        $limit_str = "$limit, $t_count";
    }
    else
    {
        $limit_str = '';
    }
    $tmp =  ($p_filter != NULL) ? $p_filter->query() : '';
    $groups = db_extract(DB_TABLE_GROUP, $tmp,
            $t_sort_field.' '.$t_sort_order, $limit_str);

    return $groups;
}

/**
 * Get unhidden groups.
 *
 * @return array groups array
 */
function get_visible_groups()
{
    return db_extract(DB_TABLE_GROUP, '`group_hidden` = 0', 'group_name');
}

/**
 * Get groups count by filter.
 *
 * @param $p_filter class group_filter
 *
 * @return int groups count
 */
function get_groups_count($p_filter)
{
    $tmp =  ($p_filter != NULL) ? $p_filter->query() : '';
    if ($tmp != '')
    {
        $tmp = ' WHERE '.$tmp;
    }

    global $cfg_language_id;
    $query = 'SELECT COUNT(*) as `_count_`
             FROM `'.DB_TABLE_GROUP.'` '.$tmp;
    global $adodb;
    $result = $adodb->Execute($query);

    $count = 0;
    if ($adodb->ErrorMsg() == '')
    {
        if (!$result->EOF)
        {
            $count = $result->fields['_count_'];
        }
    }
    return $count;
}

/**
 * Get unhidden groups.
 *
 * @return array groups array
 */
function get_unhidden_groups()
{
    $groups = db_extract(DB_TABLE_GROUP, '`group_hidden` = 0',
                '`group_name` ASC');

    return $groups;
}

/**
 * Get groups from groups id array.
 *
 * @param $p_id_array int array groups id
 *
 * @return array groups array
 */
function get_groups_from_array($p_id_array)
{
    $tmp = '';
    $array = array_values($p_id_array);
    $size = sizeof($array);

    if ($size == 0)
        return NULL;

    for($i = 0; $i < $size - 1; $i++)
        $tmp .=  db_prepare_int($array[$i]).', ';
    $tmp .= db_prepare_int($array[$size-1]);

    $groups = db_extract(DB_TABLE_GROUP, "id IN($tmp)", 'id ASC');

    return $groups;
}

/**
 * Delete group.
 *
 * @param $p_id int group id
 *
 * @return bool TRUE on success or FALSE on failure
 */
function delete_group($p_id)
{
    global $adodb;

    $t_id = db_prepare_int($p_id);

    $count = db_count(DB_TABLE_USER, '`user_group_id` = '.$t_id);
    $count += db_count(DB_TABLE_GROUP_COURSE, '`group_id` = '.$t_id);
    if ($count == 0)
    {
        $query =  'DELETE FROM `'.DB_TABLE_GROUP.'` WHERE id='.$t_id;
        $adodb->Execute($query);
        return ($adodb->ErrorMsg() == '');
    }
    else
    {
        return FALSE;
    }


}

/**
 * Delete groups.
 *
 * @param $p_id_array int array groups id array
 *
 * @return bool TRUE on success or FALSE on failure
 */
function delete_groups($p_id_array)
{
    $tmp = TRUE;
    foreach($p_id_array as $id)
    {
        $result =  delete_group($id);
        $tmp = $tmp && $result;
    }

    return $tmp;
}

/**
 * Add group.
 *
 * @param $p_name string group name
 * @param $p_description string group description
 * @param $p_hidden int group hidden
 *
 * @return adodb result
 */
function add_group($p_name, $p_description, $p_hidden)
{
    global $adodb;
    $t_name = db_prepare_string($p_name);
    $t_description = db_prepare_string($p_description);
    $t_hidden = db_prepare_int($p_hidden);

    $query = 'INSERT INTO `'.DB_TABLE_GROUP.'`'.
        '(`group_name`, `group_description`, `group_hidden`)
        VALUES('.$t_name.','.$t_description.','.$t_hidden.')';

    return $adodb->Execute($query);
}

/**
 * Get group id by group name.
 *
 * @param $p_name string group name
 *
 * @return int group id on success or 0 on failure
 */
function get_group_id($p_name)
{
    $t_name = db_prepare_string(trim($p_name));

    $groups[] = db_extract(DB_TABLE_GROUP, '`group_name` = '.$t_name);
    $id = 0;

    if (count($groups[0]) > 0)
    {
        $id = $groups[0][0]['id'];
    }

    return $id;
}

/**
 * Get group.
 *
 * @param $p_id int group id
 *
 * @return group class
 */
function get_group($p_id)
{
    $t_id = db_prepare_int($p_id);

    $groups[] = db_extract(DB_TABLE_GROUP, '`id`='.$t_id);
    $group = new group();
    if (count($groups[0]) > 0)
    {
        $group->id = $groups[0][0]['id'];
        $group->name = $groups[0][0]['group_name'];
        $group->description = $groups[0][0]['group_description'];
        $group->hidden = $groups[0][0]['group_hidden'];
    }

    return $group;
}

/**
 * Edit group.
 *
 * @param $p_id int group id
 * @param $p_name string new group name
 * @param $p_description string new group description
 * @param $p_hidden int group is hidden
 *
 * @return adodb result
 */
function edit_group($p_id, $p_name, $p_description, $p_hidden)
{
    $t_id = db_prepare_int($p_id);

    $t_name = db_prepare_string(trim($p_name));
    $t_description = db_prepare_string(trim($p_description));
    $t_hidden = db_prepare_int($p_hidden);

    $query = 'UPDATE `'.DB_TABLE_GROUP.'`'.
         ' SET `group_name` = '.$t_name.',
         `group_description` = '.$t_description.',
         `group_hidden` = '.$t_hidden.'
          WHERE `id` ='.$t_id;

    return db_exec($query);
}

